package com.lmiot.xyclick.Activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.lmiot.xyclick.Bean.SQL.AutoBean;
import com.lmiot.xyclick.Bean.SQL.AutoBeanSqlUtil;
import com.lmiot.xyclick.Bean.SQL.GroupBean;
import com.lmiot.xyclick.Bean.SQL.GroupBeanSqlUtil;
import com.lkx.liandianqinew.R;
import com.lmiot.xyclick.Util.ClickUtils;
import com.lmiot.xyclick.Util.DpUtil;
import com.lmiot.xyclick.Util.LayoutDialogUtil;
import com.lmiot.xyclick.Util.StateBarUtil;
import com.youyi.yyviewsdklibrary.Dialog.util.ToastUtil;
import com.youyi.yyviewsdklibrary.LoadingDialog;
import com.youyi.yyviewsdklibrary.View.TitleBarView;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class GroupActivity extends BaseActivity {
    private List<AutoBean> mAutoBeanList;
    private Dialog mDialog;
    private String mGroupID;
    ListView mIdListview;
    EditText mIdSearchEdit;
    private CardView mIdSearchLayout;
    TitleBarView mIdTitleBar;
    private Intent mIntent;
    private ObjAdapter mObjAdapter;
    private String mSearchName;
    private Set<String> mSet;

    /* access modifiers changed from: protected */
    @Override // android.support.v7.app.AppCompatActivity, android.support.v4.app.SupportActivity, android.support.v4.app.FragmentActivity, com.lmiot.xyclick.Activity.BaseActivity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_group);
        initView();
        StateBarUtil.immersive(this, getResources().getColor(R.color.colorAccent));
        StateBarUtil.setPadding(this, this.mIdTitleBar);
        this.mGroupID = getIntent().getStringExtra("groupID");
        GroupBean searchByID = GroupBeanSqlUtil.getInstance().searchByID(this.mGroupID);
        TitleBarView titleBarView = this.mIdTitleBar;
        titleBarView.setTitle(searchByID.getGroupName() + "");
        setFind();
        this.mIdTitleBar.setOnItemClickListener(new TitleBarView.onItemClickListener() {
            /* class com.lmiot.xyclick.Activity.GroupActivity.AnonymousClass1 */

            @Override // com.youyi.yyviewsdklibrary.View.TitleBarView.onItemClickListener
            public void onTitleClick(View view) {
            }

            @Override // com.youyi.yyviewsdklibrary.View.TitleBarView.onItemClickListener
            public void onBackClick(View view) {
                GroupActivity.this.finish();
            }

            @Override // com.youyi.yyviewsdklibrary.View.TitleBarView.onItemClickListener
            public void onMenuClick(View view) {
                GroupActivity.this.chosGroudDialog();
            }
        });
    }

    @Override // android.support.v4.app.FragmentActivity, com.lmiot.xyclick.Activity.BaseActivity
    public void onResume() {
        super.onResume();
        LoadingDialog.show(this, LoadingDialog.DialogType.CENTER, true, "");
        showListView();
    }

    private void setFind() {
        this.mIdSearchEdit.addTextChangedListener(new TextWatcher() {
            /* class com.lmiot.xyclick.Activity.GroupActivity.AnonymousClass2 */

            public void afterTextChanged(Editable editable) {
            }

            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                GroupActivity.this.mSearchName = charSequence.toString();
                if (!TextUtils.isEmpty(GroupActivity.this.mSearchName)) {
                    ArrayList arrayList = new ArrayList();
                    if (GroupActivity.this.mAutoBeanList != null && GroupActivity.this.mAutoBeanList.size() > 0) {
                        for (AutoBean autoBean : GroupActivity.this.mAutoBeanList) {
                            if (autoBean.getAutoName().contains(GroupActivity.this.mSearchName)) {
                                arrayList.add(autoBean);
                            }
                        }
                        if (GroupActivity.this.mObjAdapter != null) {
                            GroupActivity.this.mObjAdapter.setData(arrayList, GroupActivity.this.mSearchName);
                        }
                    }
                } else if (GroupActivity.this.mObjAdapter != null) {
                    GroupActivity.this.mObjAdapter.setData(GroupActivity.this.mAutoBeanList, null);
                }
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void showListView() {
        this.mAutoBeanList = AutoBeanSqlUtil.getInstance().searchAllByGroup(this.mGroupID);
        ObjAdapter objAdapter = new ObjAdapter(this.mAutoBeanList);
        this.mObjAdapter = objAdapter;
        this.mIdListview.setAdapter((ListAdapter) objAdapter);
        LoadingDialog.hidden();
    }

    private void initView() {
        this.mIdTitleBar = (TitleBarView) findViewById(R.id.id_title_bar);
        this.mIdSearchEdit = (EditText) findViewById(R.id.id_search_edit);
        this.mIdSearchLayout = (CardView) findViewById(R.id.id_search_layout);
        this.mIdListview = (ListView) findViewById(R.id.id_listview);
    }

    /* access modifiers changed from: private */
    public class ObjAdapter extends BaseAdapter {
        private List<AutoBean> autoList;

        public Object getItem(int i) {
            return null;
        }

        public long getItemId(int i) {
            return 0;
        }

        public ObjAdapter(List<AutoBean> list) {
            this.autoList = list;
        }

        public int getCount() {
            List<AutoBean> list = this.autoList;
            if (list == null) {
                return 0;
            }
            return list.size();
        }

        public View getView(int i, View view, ViewGroup viewGroup) {
            View inflate = View.inflate(GroupActivity.this, R.layout.item_group_manager, null);
            ImageView imageView = (ImageView) inflate.findViewById(R.id.id_del);
            TextView textView = (TextView) inflate.findViewById(R.id.id_name);
            final AutoBean autoBean = this.autoList.get(i);
            String autoName = autoBean.getAutoName();
            if (TextUtils.isEmpty(GroupActivity.this.mSearchName)) {
                textView.setText(autoName);
            } else {
                String str = GroupActivity.this.mSearchName;
                textView.setText(Html.fromHtml(autoName.replace(str, "<font color='#FF0000'>" + GroupActivity.this.mSearchName + "</font>")));
            }
            imageView.setOnClickListener(new View.OnClickListener() {
                /* class com.lmiot.xyclick.Activity.GroupActivity.ObjAdapter.AnonymousClass1 */

                public void onClick(View view) {
                    ClickUtils.onlyVibrate(GroupActivity.this);
                    if (GroupActivity.this.mGroupID.equals("666")) {
                        ToastUtil.warning("默认分组不允许删除！");
                        return;
                    }
                    autoBean.setGroupID("666");
                    AutoBeanSqlUtil.getInstance().add(autoBean);
                    GroupActivity.this.showListView();
                }
            });
            return inflate;
        }

        public void setData(List<AutoBean> list, String str) {
            this.autoList = list;
            GroupActivity.this.mSearchName = str;
            notifyDataSetChanged();
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void chosGroudDialog() {
        HashSet hashSet = new HashSet();
        this.mSet = hashSet;
        hashSet.clear();
        Dialog dialog = this.mDialog;
        if (dialog != null && dialog.isShowing()) {
            this.mDialog.dismiss();
        }
        Dialog createDailog = LayoutDialogUtil.createDailog(this, R.layout.dialog_chose_group);
        this.mDialog = createDailog;
        ListView listView = (ListView) this.mDialog.findViewById(R.id.id_listivew);
        ((TextView) createDailog.findViewById(R.id.tv_title)).setText("请选择要导入的自动化");
        List<AutoBean> searchAllByGroupOther = AutoBeanSqlUtil.getInstance().searchAllByGroupOther(this.mGroupID);
        if (searchAllByGroupOther.size() > 5) {
            listView.setLayoutParams(new LinearLayout.LayoutParams(-1, DpUtil.dip2px(this, 300.0f)));
        } else {
            listView.setLayoutParams(new LinearLayout.LayoutParams(-1, -2));
        }
        listView.setAdapter((ListAdapter) new AutoAdapter(searchAllByGroupOther));
        ((TextView) this.mDialog.findViewById(R.id.tv_sure)).setOnClickListener(new View.OnClickListener() {
            /* class com.lmiot.xyclick.Activity.GroupActivity.AnonymousClass3 */

            public void onClick(View view) {
                for (String str : GroupActivity.this.mSet) {
                    AutoBean searchByID = AutoBeanSqlUtil.getInstance().searchByID(str);
                    searchByID.setGroupID(GroupActivity.this.mGroupID);
                    AutoBeanSqlUtil.getInstance().add(searchByID);
                }
                GroupActivity.this.showListView();
                GroupActivity.this.mDialog.dismiss();
            }
        });
        ((TextView) this.mDialog.findViewById(R.id.tv_cancel)).setOnClickListener(new View.OnClickListener() {
            /* class com.lmiot.xyclick.Activity.GroupActivity.AnonymousClass4 */

            public void onClick(View view) {
                GroupActivity.this.mDialog.dismiss();
            }
        });
    }

    /* access modifiers changed from: private */
    public class AutoAdapter extends BaseAdapter {
        List<AutoBean> list;

        public Object getItem(int i) {
            return null;
        }

        public long getItemId(int i) {
            return 0;
        }

        public AutoAdapter(List<AutoBean> list2) {
            this.list = list2;
        }

        public int getCount() {
            return this.list.size();
        }

        public View getView(int i, View view, ViewGroup viewGroup) {
            View inflate = View.inflate(GroupActivity.this, R.layout.item_chose_group_auto, null);
            ImageView imageView = (ImageView) inflate.findViewById(R.id.id_checked);
            ImageView imageView2 = (ImageView) inflate.findViewById(R.id.id_unchecked);
            ((TextView) inflate.findViewById(R.id.id_name)).setText(this.list.get(i).getAutoName());
            final String autoID = this.list.get(i).getAutoID();
            if (GroupActivity.this.mSet.contains(autoID)) {
                imageView.setVisibility(View.VISIBLE);
                imageView2.setVisibility(View.GONE);
            } else {
                imageView.setVisibility(View.GONE);
                imageView2.setVisibility(View.VISIBLE);
            }
            inflate.setOnClickListener(new View.OnClickListener() {
                /* class com.lmiot.xyclick.Activity.GroupActivity.AutoAdapter.AnonymousClass1 */

                public void onClick(View view) {
                    if (GroupActivity.this.mSet.contains(autoID)) {
                        GroupActivity.this.mSet.remove(autoID);
                    } else {
                        GroupActivity.this.mSet.add(autoID);
                    }
                    AutoAdapter.this.notifyDataSetChanged();
                }
            });
            return inflate;
        }
    }
}
